﻿using System;
using System.IO;
using UnityEngine;
namespace NuoYan.Extension
{
    #region 计时器
    /// <summary>
    /// 依赖Update
    /// </summary>
    public class Timer
    {
        private float duration;
        private float lastTime;
        private bool isRunning;
        public bool isFinished { get; private set; }
        public Timer(float duration)
        {
            this.duration = duration;
        }
        public void Start()
        {
            if (!isRunning)
            {
                isRunning = true;
                isFinished = false;
                lastTime = Time.time;
            }
        }
        /// <summary>
        /// 循环计时器
        /// </summary>
        public void UpdateTime()
        {
            if (isRunning)
            {
                if (Time.time - lastTime > duration)
                {
                    isFinished = true;
                    lastTime = Time.time;
                }
                else
                {
                    isFinished = false;
                }
            }

        }

        public void Stop()
        {
            if (isRunning)
            {
                isRunning = false;
                isFinished = false;
                lastTime = Time.time;
            }

        }

        public void RestTime()
        {
            lastTime = Time.time;
            isRunning = false;
            isFinished = false;
            Start();
        }
    }
    #endregion


    #region 时间格式
    /************************************格式化输出时间******************************************/
    /// <summary>
    /// 时间格式化输出  例：2024年9月28日11时23分58秒
    /// </summary>
    [Serializable]
    public struct TimeFormat
    {
        /// <summary>
        /// 年
        /// </summary>
        public int Year;
        /// <summary>
        /// 月
        /// </summary>
        public int Month;
        /// <summary>
        /// 日
        /// </summary>
        public int Day;
        /// <summary>
        /// 时
        /// </summary>
        public int Hour;
        /// <summary>
        /// 分
        /// </summary> 
        public float Minute;
        /// <summary>
        /// 秒
        /// </summary>
        public float Second;
        /// <summary>
        /// 格式化输出的时间  例：2024年9月28日11时23分58秒
        /// </summary>
        public string ChineseFormatTime => $"{Year}年{Month}月{Day}日{Hour}时{Minute}分{Second}秒";
        public string EnglishFormatTime => $"{Year}-{Month}-{Day} {Hour}:{Minute}:{Second}";
    }
    public class Clock
    {
        /// <summary>
        /// 格式化输出本地时间
        /// </summary>
        /// <returns></returns>
        public static TimeFormat ReadTime()
        {
            TimeFormat timeFormat = new TimeFormat();
            string time = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
            string[] timeSplit = time.Split('_');
            timeFormat.Year = int.Parse(timeSplit[0]);
            timeFormat.Month = int.Parse(timeSplit[1]);
            timeFormat.Day = int.Parse(timeSplit[2]);
            timeFormat.Hour = int.Parse(timeSplit[3]);
            timeFormat.Minute = int.Parse(timeSplit[4]);
            timeFormat.Second = int.Parse(timeSplit[5]);
            return timeFormat;
        }
        /// <summary>
        /// 根据传入的时间字符串格式化输出时间 字符间隔必须为：_  格式  "yyyy_MM_dd_HH_mm_ss"
        /// </summary>
        /// <param name="time">时间，需要为"yyyy_MM_dd_HH_mm_ss"形式</param>
        /// <returns></returns>
        public static TimeFormat ReadTime(string time)
        {
            TimeFormat timeFormat = new TimeFormat();
            string[] timeSplit = time.Split(separator: '_');
            timeFormat.Year = int.Parse(timeSplit[0]);
            timeFormat.Month = int.Parse(timeSplit[1]);
            timeFormat.Day = int.Parse(timeSplit[2]);
            timeFormat.Hour = int.Parse(timeSplit[3]);
            timeFormat.Minute = int.Parse(timeSplit[4]);
            timeFormat.Second = int.Parse(timeSplit[5]);
            return timeFormat;
        }
        /// <summary>
        /// 读取文件夹最后修改时间
        /// </summary>
        /// <param name="fileInfo">文件引用</param>
        /// <returns></returns>
        public static TimeFormat ReadTime(FileInfo fileInfo)
        {
            TimeFormat timeFormat = new TimeFormat();
            string time = fileInfo.LastWriteTime.ToString("yyyy_MM_dd_HH_mm_ss");
            string[] timeSplit = time.Split('_');
            timeFormat.Year = int.Parse(timeSplit[0]);
            timeFormat.Month = int.Parse(timeSplit[1]);
            timeFormat.Day = int.Parse(timeSplit[2]);
            timeFormat.Hour = int.Parse(timeSplit[3]);
            timeFormat.Minute = int.Parse(timeSplit[4]);
            timeFormat.Second = int.Parse(timeSplit[5]);
            return timeFormat;
        }
        /// <summary>
        /// 逆转化时间，将 年月日时分秒形式转换为 yyyy_MM_dd_HH_mm_ss 形式
        /// </summary>
        /// <param name="time">需要转化的时间，需要为 年月日时分秒 的形式</param>
        /// <returns></returns>
        public static string ConvertTime(string time)
        {
            time = time.Replace("年", "_").Replace("月", "_").Replace("日", "_").Replace("时", "_").Replace("分", "_").Replace("秒", "");
            string realTime = time.ToString();
            return realTime;
        }
    }
    #endregion
}

